//
//  PackageViewController.m
//  werfgh
//
//  Created by 李晓勇 on 14-9-12.
//  Copyright (c) 2014年 dingshenwangluokeji. All rights reserved.
//

#import "PackageViewController.h"
#import "NetAccess.h"
#import "MBProgressHUD.h"
#import "AFNetworking.h"
@interface PackageViewController ()




@end

@implementation PackageViewController
{
    MBProgressHUD *pro;
    NetAccess *_net;
    NSString *_word;
    NSString *_name;
    NSUserDefaults *users;
    NSMutableData *_data;
    UILabel *_label;
    UILabel *_label1;
    UILabel *_label2;
    float _string;
    NSArray*arry;
    UITextField *textf;
    UITextField *textf1;
    UITextField *textf2;
    
    
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
//-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
//{
//    _data =[[NSMutableData alloc]init];
//}
//-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
//{
//    [_data appendData:data];
//    
//}
//-(void)connectionDidFinishLoading:(NSURLConnection *)connection
//{
//    NSDictionary *rootDict =[NSJSONSerialization JSONObjectWithData:_data options:NSJSONReadingMutableContainers error:nil];
//    NSArray *arrays =[rootDict objectForKey:@"infoCustomers"];
//    if(arrays.count==0)
//    {[pro removeFromSuperview];
//    }else{
//        NSDictionary *ed =[arrays objectAtIndex:0];
//        NSString *gu =[ed objectForKey:@"name"];
//        _string =[[ed objectForKey:@"money"] integerValue];
//        [pro removeFromSuperview];
//        _label2.text =[NSString stringWithFormat:@"话费余额:  %f元",_string];
//        _label1.text =[NSString stringWithFormat:@"手机号:  %@",gu];
//    }
//}
-(void)match
{
    pro = [[MBProgressHUD alloc] initWithView:self.view];
    pro.labelText = @"请稍后..";
    pro.mode = MBProgressHUDModeText;
    [self.view addSubview:pro];
    [pro showAnimated:YES whileExecutingBlock:^{
        [NSThread sleepForTimeInterval:2.0];
    } completionBlock:^{
        
    }];
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self match];
    if(![_net haveNet])
    {
        UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"提示" message:@"无网络连接" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
        [pro removeFromSuperview];
    }
    else{
        [[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(getStarte:) object:nil];
        [self performSelector:@selector(getStarte:) withObject:nil afterDelay:2.0f];
    }
}
-(void)getStarte:(id)sender
{
    //    NSURL *url =[NSURL URLWithString:[NSString stringWithFormat:more,_word]];
    //    NSURLRequest* request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:30.0];
    //    NSURLConnection *connent =[[NSURLConnection alloc]initWithRequest:request delegate:self];
    //    [connent start];
    NSString *str=[NSString stringWithFormat:@"http://oemapi2.leehuu.cn/vos2/api/UserInfo?username=%@&Password=%@",_name,_word];
    NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *html = operation.responseString;
        NSData* data=[html dataUsingEncoding:NSUTF8StringEncoding];
        id dict=[NSJSONSerialization  JSONObjectWithData:data options:0 error:nil];
        NSLog(@"获取到的数据为：%@",dict);
        arry=[dict objectForKey:@"infoSuiteOrders"];
        
        
        UIButton*button=[UIButton buttonWithType:UIButtonTypeCustom];
        button.frame=CGRectMake(20, 200, 100, 40);
        [button setTitle:@"套餐ID" forState:UIControlStateNormal ];
        //
        button.backgroundColor=[UIColor redColor];
        
        [self.view addSubview:button ];
        textf=[[UITextField alloc]initWithFrame:CGRectMake(200,200, 100, 40)];
        
        textf.borderStyle=UITextBorderStyleLine;
        textf.userInteractionEnabled=NO;
        [self.view addSubview:textf];
        [button addTarget:self action:@selector(tap:) forControlEvents:UIControlEventTouchUpInside];
        UIButton*button1=[UIButton buttonWithType:UIButtonTypeCustom];
        button1.frame=CGRectMake(20, 250, 100, 40);
        [button1 setTitle:@"生效时间" forState:UIControlStateNormal ];
        //
        button1.backgroundColor=[UIColor redColor];
        
        [self.view addSubview:button1 ];
        textf1=[[UITextField alloc]initWithFrame:CGRectMake(200,250, 100, 40)];
        
        textf1.borderStyle=UITextBorderStyleLine;
        textf1.userInteractionEnabled=NO;
        [self.view addSubview:textf1];
        [button1 addTarget:self action:@selector(tap:) forControlEvents:UIControlEventTouchUpInside];
        
        UIButton*button2=[UIButton buttonWithType:UIButtonTypeCustom];
        button2.frame=CGRectMake(20, 300, 100, 40);
        [button2 setTitle:@"到期时间" forState:UIControlStateNormal ];
        //
        button2.backgroundColor=[UIColor redColor];
        
        [self.view addSubview:button2 ];
        textf2=[[UITextField alloc]initWithFrame:CGRectMake(200,300, 100, 40)];
        
        textf2.borderStyle=UITextBorderStyleLine;
        textf2.userInteractionEnabled=NO;
        [self.view addSubview:textf2];
        [button2 addTarget:self action:@selector(tap:) forControlEvents:UIControlEventTouchUpInside];
        button.tag=10;
        button1.tag=11;
        button2.tag=12;
        textf2.textAlignment=1;
        textf.textAlignment=1;
        textf1.textAlignment=1;
        
        
        
        
        
    }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"发生错误！%@",error);
    }];
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [queue addOperation:operation];
    
}
-(void)tap:(UIButton*)sender
{
    
    
    switch (sender.tag) {
        case 10:
            textf.text =[[arry[0] objectForKey:@"suiteId"] stringValue];
            break;
        case 11:
        {
            NSString*str =[[arry[0] objectForKey:@"availableTime"] stringValue];
            textf1.text= [self switchTime:str];
        }
         
            break;
            
        case 12:
            if
                ([[[arry[0] objectForKey:@"expireTime"]stringValue] isEqualToString:@"9223372036854775807"])
            {
                textf2.text=@"无限制";
            }
            else
            {
             NSString*str=[[arry[0] objectForKey:@"expireTime"] stringValue];
                textf2.text= [self switchTime:str];

            }
            
        default:
            break;
    }
    
}
-(NSString*)switchTime:(NSString*)timeStr
{
    NSDate *d = [NSDate dateWithTimeIntervalSince1970:[timeStr doubleValue]/1000];
    // NSLog(@"dddd:%@",d);
    //2011-01-18 13:00:00 +0000
    NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init];
    [formatter1 setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *showtimeNew = [formatter1 stringFromDate:d];
    // NSLog(@"showtimeNew:%@",showtimeNew);        // NSArray *lists =[d]
    return showtimeNew;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    self.view.backgroundColor =[UIColor whiteColor];
    _net = [NetAccess sharedNetAccess];
    [self controllersd];
    
    UIImageView *imageView =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"icon.png"]];
    imageView.frame =CGRectMake(110, 90 ,90, 80);
    [self.view addSubview:imageView];
    
    
    //
    //    _label =[[UILabel alloc]init];
    //    _label.frame=CGRectMake(60, imageView.frame.origin.y+80, 260, 35);
    //    _label.backgroundColor =[UIColor whiteColor];
    //    [self.view addSubview:_label];
    //    _label1 =[[UILabel alloc]init];
    //    _label1.frame=CGRectMake(60, _label.frame.origin.y+40, 260, 35);
    //    _label1.backgroundColor =[UIColor whiteColor];
    //    [self.view addSubview:_label1];
    //    _label2 =[[UILabel alloc]init];
    //    _label2.frame=CGRectMake(60, _label1.frame.origin.y+40, 260, 35);
    //    _label2.backgroundColor =[UIColor whiteColor];
    //    [self.view addSubview:_label2];
}
-(void)controllersd
{
    users =[NSUserDefaults standardUserDefaults];
    NSArray *array =[users objectForKey:@"newusers1"];
    NSDictionary *dict =[array objectAtIndex:0];
    NSLog(@"dadadadada%@",dict);
    
    _name =[dict objectForKey:@"username"];
    _word=[dict objectForKey:@"userword"];
    
}
-(void)btnClick
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
